﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Resources;
using Smatime.Web.DAO;
using Smatime.Web.Models;

namespace Smatime.Web.Controllers
{
    public class CompanyStaffController : ApiController
    {
        // GET api/companystaff
        public IEnumerable<CompanyStaff> Get()
        {
            return new CompanyStaffRepository().GetList();
        }

        // GET api/companystaff/5
        public CompanyStaff Get(int id)
        {
            return new CompanyStaffRepository().GetById(id);
        }
        public HttpResponseMessage Post([FromBody] CompanyStaff value)
        {
            try
            {
                var repos = new CompanyStaffRepository();
                var staffStoreRepos = new StaffStoreRepository();

                // If the id's staff is not set => insert.
                if (value.Id == DataHelper.NotSet)
                {
                    // LH: Insert company staff into database
                    repos.Insert(value);
                    //LH: Get the inserted staff by company id, name, namekana
                    var lastStaffInsertd = new LastStaffInsertedRepository();
                    var currentStaff = lastStaffInsertd.GetCompanyStaffBy(value.CompanyId, value.Name, value.NameKana);
                    // return the id of inserted staff.

                    if (currentStaff.IsNull())
                    {
                        Request.CreateResponse(HttpStatusCode.BadRequest, Texts.CannotCreateCompanyStaff);
                    }
                    foreach (var storeId in value.StoreIds)
                    {
                        var staffStore = new StaffStore
                        {
                            CompanyStaffId = currentStaff.Id,
                            CompanyStoreId = storeId
                        };
                        staffStoreRepos.Insert(staffStore);
                    }
    
                    return Request.CreateResponse(HttpStatusCode.OK, currentStaff.Id);
                }
                repos.Update(value);
                return Request.CreateResponse(HttpStatusCode.OK);
            }
            catch (Exception exception)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest, exception);
            }
        }

        // PUT api/companystaff/5
        [HttpPut]
        public HttpResponseMessage Put(int id, [FromBody] CompanyStaff value)
        {  
            try
            {
                //Update CompanyStaff properties
                var repos = new CompanyStaffRepository();
                value.Id = id;
                repos.Update(value);

                //Update to Store list of the companyStaff
                var staffStoreRepos = new StaffStoreRepository();
                staffStoreRepos.UpdateStoreListByStaff(id, value.StoreIds);

                return Request.CreateResponse(HttpStatusCode.OK);
            }
            catch (Exception exception)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest, exception);
            }
        }

        // DELETE api/companystaff/5
        public void Delete(int id)
        {
        }
    }
}